package leetcode;

/*
206. 反转链表
反转一个单链表。
示例:
输入: 1->2->3->4->5->NULL
输出: 5->4->3->2->1->NULL
*/

public class problems_206 {

    public static void main(String[] args) {
        ListNode node1 = new ListNode(1);
        ListNode node2 = new ListNode(2);
        ListNode node3 = new ListNode(3);
        ListNode node4 = new ListNode(4);
        ListNode node5 = new ListNode(5);
        ListNode node6 = new ListNode(6);
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;
        node5.next = node6;
        System.out.println(new Solution().reverseList(node1));
    }
    static public class ListNode {
        int val;
        ListNode next;
        ListNode(int x) {
            val = x;
        }
    }
    static class Solution {
        public ListNode reverseList(ListNode head) {
            if (null == head) return null;
            // 三指针
            ListNode prev = null; // 前指针
            ListNode cur = head; // 当前指针
            ListNode next = head.next; // 后指针
            while (null != cur){
                cur.next = prev;
                prev = cur;
                cur = next;
                if (null != next) next = next.next;
            }
            return prev;
        }
    }
}